<script type="text/javascript">
/**
 * ------------------------------------------------------
 * 编辑页面，需要避免重复加载
 * ------------------------------------------------------
 */
$(function(){
	var regex = {$Think.lang.CONTENT_REGEX};
	var editName="文章编辑";
	var editorOption = {
			toolbars:[['fullscreen','source', '|', 'undo', 'redo', '|',
		                'bold', 'italic', 'underline','removeformat', 'formatmatch', 'autotypeset','|', 'forecolor', 'backcolor', '|',
		                'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
		             	'fontfamily', 'fontsize', '|',
		                'indent', '|','justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
		                'link', 'unlink', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
		                'insertimage', 'emotion', 'insertvideo', 'music','attachment', 'map', 'template', '|',
		                'horizontal', 'spechars', '|','inserttable', 'deletetable', 'insertparagraphbeforetable', 
		                'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 
		                'splittocells', 'splittorows', 'splittocols', 'charts', '|',
		                'preview', 'searchreplace', 'help']],
	      	wordCount:true,
	        textarea:'post_edit_text',
	        autoHeightEnabled:false,
	        elementPathEnabled:false,
	        pasteplain:false,
	        initialFrameHeight:330,
	        initialFrameWidth:840
    };
	var edit_editor = new baidu.editor.ui.Editor(editorOption);
    edit_editor.render("post_edit_text");
    edit_editor.addListener("ready", function () {
    	edit_editor.setContent($("#post_edit_content_hidden").html());
    });

    $("#post_edit_module_check").click(function(){
    	if($(this).is(":checked")){
    		$(".modules_container select").attr("disabled",false);
    	}else{
    		$(".modules_container select").attr("disabled",true);
    	}
    });
    
    $("#post_edit_module_check").trigger("click");
    
    $("#post_edit_submit").button().click(function(){
    	var id=$("#post_edit_id").val();
    	var title=$("#post_edit_title").val();
		//var module=$("#post_edit_module").val()
		var content=edit_editor.getContent();
		if(parseInt(id)<=0){
			artError("无法提交，请刷新后重试！");
			return;
		}
		if(title.length==0){
			showTip($("#post_edit_title"),"请输入内容标题！");
			return;
		}
		hideTip($("#post_edit_title"));
		if(!edit_editor.hasContents()){
			showTip($("#post_edit_text"),"请输入内容正文！");
			return;
		}
		hideTip($("#post_edit_text"));
		artConfirm("您确定修改吗？",function(){
			var module=$(".modules_select:last").val()
			if($("#post_edit_module_check").is(":checked")){
				data={'tid':id,'module':module,'title':title,'content':content};
			}else{
				data={'tid':id,'title':title,'content':content};
			}
			$("#post_edit_submit").button("disable")
			$.post(BASE+"/admin/content/post_edit",data,function(e){
				if(e.status==1){
					artSuccess("修改成功！",0,function(){
						//location.reload();
					});
				}else{
					artError(e.info);
				}
				$("#post_edit_submit").button("enable")
			},"json")
		},function(){
			return;
		})
    })
    
	$("#post_edit_reset").button().click(function(){
		artConfirm("您确定还原所有内容吗？",function(){
			reset();
			return;
		},function(){
			return;
		})
	})
	
	function reset(){
    	$("#post_edit_title").val($("#post_edit_title_hidden").val());
		//$("#post_edit_module option[value='"+$("#post_edit_mid_hidden").val()+"']").prop("selected", 'selected');
		edit_editor.setContent($("#post_edit_content_hidden").html());
    }
	
	$("#post_edit_import").button().click(function(){
		var url=$("#post_edit_url").val();
		if(url.length==0){
			showTip($("#post_edit_url"),"请输入内容标题！","center","bottom");
		}else{
			hideTip($("#post_edit_url"));
			var result=url.match(regex);
			if(result==null||result[2]==null){
				artError("请输入正确的网址");
			}else{
				var tid=result[2];
				var menuid=$("#menu").find("td[title='"+editName+"']").prev().html();
				openTab(menuid,editName,BASE+"/admin/content/post_edit?tid="+tid,function(){},true)
			}
		}
	});
})
</script>

<div id="show_post_edit" class="main" width="850">
	<div class="main_left">
		<input type="hidden" id="post_edit_id" value="<notempty name='post'>{$post.id}</notempty>">
		<div class="title_container">
			<label><span class="title_label">{$Think.lang.CONTENT_URL}</span><span><input type="text" name="url" maxlength="100" id="post_edit_url" 
			value="<notempty name='post'>{$Think.lang.CONTENT_DEFAULT_URL}{$post.mid}/{$post.id}</notempty>"></span></label>
			<span>&nbsp;&nbsp;<button id="post_edit_import">{$Think.lang.CONTENT_IMPORT}</button></span>
		</div>
		<div class="title_container">
			<label><span class="title_label">{$Think.lang.CONTENT_TITLE}</span><span><input type="text" name="title" maxlength="50" id="post_edit_title" value="<notempty name='post'>{$post.title}</notempty>"></span></label>
		</div>
		<div class="title_container" id="post_edit_module" value="{$post['mid']}">
			<span class="title_label">{$Think.lang.BELONG_MODULE}</span>
				<em><notempty name="current_module">{$current_module['name']}<else/>{$Think.lang.NULL}</notempty></em>
				&nbsp;
				<input type="checkbox" id="post_edit_module_check" checked="checked"/>&nbsp;{$Think.lang.RESELECT}
				<include file="module_select"/>
				<!-- 
				<span>
					<select id="post_edit_module">
						<foreach name="module" item="m">
							<if condition="isset($post) and $post[mid] eq $m[id]">
								<option value="{$m.id}" selected="selected">{$m.name}</option>
							<else/>
								<option value="{$m.id}" >{$m.name}</option>
							</if>
						</foreach>
					</select>
				</span> -->
		</div>
		<div class="text_container">
			<!-- Ueditor -->
			<div id="post_edit_text"></div>
		</div>
	</div>
	<div class="main_right">
		{$Think.lang.TIP_H3}
		{$Think.lang.POST_EDIT_TIP}
	</div>
	<div class="post_btns">
		<button id="post_edit_submit" >{$Think.lang.EDIT}</button>
		<button id="post_edit_reset" >{$Think.lang.BACK}</button>
	</div>
	<input type="hidden" id="post_edit_title_hidden" value="<notempty name='post'>{$post.title}</notempty>"/>
	<!-- <input type="hidden" id="post_edit_mid_hidden" value="<notempty name='post'>{$post.mid}</notempty>"/>-->
	<div class="hidden" id="post_edit_content_hidden">
		<notempty name='post'>{$post.content}</notempty>
	</div>
</div>